#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("合力计算");
    setFixedSize(870,900);
    connect(ui->add, SIGNAL(clicked(bool)), this, SLOT(add_clicked()));
    connect(ui->cal, SIGNAL(clicked(bool)), this, SLOT(calforce()));
    connect(ui->del, SIGNAL(clicked(bool)), this, SLOT(del_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//添加力
void MainWindow::add_clicked()
{
    //qDebug()<<"add";
    forcelabel = new QLabel(ui->scrollAreaWidget);
    forcelabel->setGeometry(10,labely,90,40);
    QString str="力"+str.number(forcenum+1);
    forcelabel->setText(str);
    f.setPixelSize(24);
    forcelabel->setFont(f);
    forcelabellist.push_back(forcelabel);
    forcelabel->show();

    force = new QTextEdit(ui->scrollAreaWidget);
    force->setText("0");
    force->setGeometry(130,labely,90,40);
    forcelist.push_back(force);
    force->show();

    forceangle = new QTextEdit(ui->scrollAreaWidget);
    forceangle->setText("0");
    forceangle->setGeometry(310,labely,90,40);
    anglelist.push_back(forceangle);
    forceangle->show();


    forcelabel = new QLabel(ui->scrollAreaWidget);
    forcelabel->setGeometry(400,labely,90,40);
    forcelabel->setText("π");
    f.setPixelSize(24);
    forcelabel->setFont(f);
    pilabellist.push_back(forcelabel);
    forcelabel->show();
    labely+=50;
    if(labely>ui->scrollAreaWidget->geometry().height())
    {
        ui->scrollAreaWidget->setGeometry(0,140,500,ui->scrollAreaWidget->geometry().height()+50);
    }
    forcenum+=1;
}

//计算合力
void MainWindow::calforce()
{
    double ForceX=0,ForceY=0;
    double Force,ForceAngle;
    for(int i=0;i<forcenum;i++)
    {
       Force=forcelist[i]->toPlainText().toDouble();
       ForceAngle=(anglelist[i]->toPlainText().toDouble())*3.1415926535;

       ForceX+=Force*cos(ForceAngle);
       ForceY+=Force*sin(ForceAngle);
    }
    double FinForce,FinForceAngle;
    FinForce=sqrt(ForceX*ForceX+ForceY*ForceY);
    FinForceAngle=acos(ForceX/FinForce)/3.1415926535;
    ui->force->setNum(FinForce);
    QString str=str.number(FinForceAngle)+"π";
    ui->forceangle->setText(str);
}

//删除力
void MainWindow::del_clicked()
{
    forcelist[forcenum-1]->hide();
    anglelist[forcenum-1]->hide();
    forcelabellist[forcenum-1]->hide();
    pilabellist[forcenum-1]->hide();
    forcelist.removeLast();
    forcelabellist.removeLast();
    anglelist.removeLast();
    pilabellist.removeLast();
    forcenum-=1;
    labely-=50;
}
